"  TabUtils: - Some useful tab tools for vim
"  -----------------------------------------
"  Version: 0.01
"  Author: Albie Janse van Rensburg
"  Date Last Modified: 2010-04-26
"  Description:
"  	A plugin that eases the use of tabs in Vim.
"  	Microsoft Windows-specific.

" (Init) {{{1
if 1==2
	if exists('g:loaded_tabutils') || &compatible
		finish
	endif
	let g:loaded_tabutils = 1
endif

" Buffer - Open the given filename (if already opened in a tab/buffer, goto tab/buffer instead) {{{1
function! Buffer(filename)
	let filename = a:filename
	if substitute(filename, ' ', '', 'g') != ""
		execute "tab drop ".filename
	endif
endfunction
command! -nargs=1 Buffer call Buffer(<args>)

" WindowsEscape - Escape a string for Windows purposes {{1
function! WindowsEscape(filename)
	let filename = substitute(a:filename, '\(^\|[^\\]\)\zs\\\ze\([^\\]\|$\)', '\\', 'g')
	let filename = substitute(filename, '\(^\|[^\\]\)\zs ', '\\ ', 'g')
	return filename
endfunction

" Buffers - See all buffers and switch to one in its current position {{{1
" nnoremap __ :buffers<BAR>let i = input("Buffer number: ")<BAR>execute "let i = bufname(".i.")"<BAR>let i = substitute(i, " ", "\\\\ ", "g")<BAR>execute "tab drop ".i<CR>
function! Buffers(...)
	let bufnamed = 0
	if a:0 >= 1
		let i = a:1
		" fix \ and spaces (for Windows purposes)
		let filename = WindowsEscape(i)
		let filename = bufname(filename)
		let filename = WindowsEscape(filename)
		if filename == ""
			let bufnamed = 1
		endif
	else
		" Present a list of buffers to the user
		buffers
		let i = input("Load buffer number? ")
		if strlen(i) > 0
			let filename = i
			execute "let filename = bufname(".filename.")"
			let filename = WindowsEscape(filename)
			if substitute(filename, '\(^\ \+\|\ \+$\)', '', 'g') == ""
				let bufnamed = 1
			endif
		else
			let bufnamed = 1
		endif
	endif
	if bufnamed == 0
		call Buffer(filename)
	endif
endfunction
command! -nargs=? Buffers call Buffers(<args>)

" Shortcuts {{{1
nnoremap <silent> <leader>bb :Buffers<CR>

" (Modelines) {{{1
" vim: ts=4 nowrap fdm=marker
